/*
 * COMP6471 Project1
 * 
 * Assignment 1: Due 10:00 Thursday 29 September 2011 
 * Submit a .tar.gz file with your Java program, test input files, and test outputs to Project 1 of electronic submission system. 
 * A batch Java program that checks the validity of a game description in terms of syntax, and checks (using brute force search of all answers) that the game has a unique solution. For 4-by-4 games. 
 * Create a collection of at least 10 4-by-4 games on which to test your program.
 * 
 * This file is created by Yuan Tao (ewan.msn@gmail.com)
 * Licensed under GNU GPL v3
 * 
 * $Author: ewan.msn@gmail.com $
 * $Date: 2011-09-29 04:54:28 +0000 (Thu, 29 Sep 2011) $
 * $Rev: 23 $
 * $HeadURL: http://comp6471.googlecode.com/svn/Project1/src/Project1Main.java $
 * 
 */

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import kklib.*;
import util.*;

public class Project1Main {
	
	public static ArrayList<Layout> layouts = new ArrayList<Layout>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Hi, wellcome to COMP6471 Project1. v0.1");
		System.out.println("Developed by Yuan Tao (ewan.msn@gmail.com)\n");
		System.out.println("Please put all the input files under the root directoy of $ThisProgram\\input\\");
		System.out.println("There are already some sample txt files under $input directory.");
		System.out.println("The result of the progrom will store in the file: $ThisProgram\\output\\result.txt \n");
		System.out.println("Then press any key to begin...");
		
		try {
			System.in.read();
		} catch (IOException e) {
			
		}

		// create and initialize the logger
		logger.init();

		// format the result.txt file.
		SimpleDateFormat tmpDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datetimeNow = tmpDate.format(new java.util.Date());
		logger.output(datetimeNow + "\nThe result is:" + "\n");
		
		// load all txt files
		InputLoader txtLoader = new InputLoader();
		String path = System.getProperty("user.dir") + "\\input";
		if (txtLoader.loadTextFiles(path, layouts) != 0) {
			return;
		}

		// checking
		Layout layout = null;
		for (int i = 0; i < layouts.size(); i++) {
			layout = layouts.get(i);

			// logging
			logger.info(layout.fileFullname);
			logger.info(layout.txtFileData.toString());

			// parse one file
			if (txtLoader.fileDataParser(layout) != 0) {
				continue;
			}
			// check the syntax of the cages and cells.
			if (layout.syntaxChecker() != 0) {
				continue;
			}
			
			// check if the layout has unique solution
			int cnt = layout.getNumOfPossibleSolutions();
			if (cnt < 0) {
				if (cnt == -5) {
					logger.solutionOK(layout.fileFullname, layout.getNumOfSolutions(), layout.gameSize);
					logger.output("StackOverFlow, There may have more solutions for the last one.");
				} else
					logger.solutionErr(layout.fileFullname, "Fail to get possible solutions");
			}
			else {
				logger.solutionOK(layout.fileFullname, cnt, layout.gameSize);
			}
		}
		

		logger.info("The program ends successfully!");
	}

}
